package plus.eadydo.gateway;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.cors.reactive.CorsUtils;
import org.springframework.web.server.WebFilter;
import plus.easydo.starter.web.core.annotation.EasyDoEnableOpenFeignClients;

import java.util.stream.Collectors;


/**
 * 网关 gateway
 *
 * @author wangxx
 */
@SpringBootApplication
@EnableDiscoveryClient
@EasyDoEnableOpenFeignClients
public class GatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
        System.out.println("" +
                "███████╗ █████╗ ███████╗██╗   ██╗     ██████╗  ██████╗\n" +
                "██╔════╝██╔══██╗██╔════╝╚██╗ ██╔╝     ██╔══██╗██╔═══██╗\n" +
                "█████╗  ███████║███████╗ ╚████╔╝█████╗██║  ██║██║   ██║\n" +
                "██╔══╝  ██╔══██║╚════██║  ╚██╔╝ ╚════╝██║  ██║██║   ██║\n" +
                "███████╗██║  ██║███████║   ██║        ██████╔╝╚██████╔╝\n" +
                "╚══════╝╚═╝  ╚═╝╚══════╝   ╚═╝        ╚═════╝  ╚═════╝\n" +
                "  ┌─┐┌┬┐┌─┐┬─┐┌┬┐  ┌─┐┬ ┬┌─┐┌─┐┌─┐┌─┐┌─┐  ┬\n" +
                "  └─┐ │ ├─┤├┬┘ │   └─┐│ ││  │  ├┤ └─┐└─┐  │\n" +
                "  └─┘ ┴ ┴ ┴┴└─ ┴   └─┘└─┘└─┘└─┘└─┘└─┘└─┘  o");
    }


    /**
     * gateway使用openfeign需要手动注入HttpMessageConverters
     * @param converters converters
     * @return HttpMessageConverters
     */
    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }


    /**
     * 解决跨域
     * @return WebFilter
     */
    @Bean
    public WebFilter corsFilter() {
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            if (!CorsUtils.isCorsRequest(request)) {
                return chain.filter(exchange);
            }
            ServerHttpResponse response = exchange.getResponse();
            HttpHeaders headers = response.getHeaders();
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST,GET,OPTIONS,DELETE,PUT");
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "x-auth-token,Origin,Access-Token,X-Requested-With,X_Requested_With,Content-Type,token,Accept,authorization");
            headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "3628800");
            headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "authorization,token");
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "TRUE");
            return chain.filter(exchange);
        };
    }

}
